//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using OpenLS.Drawing;

namespace OpenLS.Chart.UI
{
    using conv = System.Convert;
    class SurfaceFactory: IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Surface surface = StrictConverter.Convert<Surface>(values[0]);
            ChartArea area = StrictConverter.Convert<ChartArea>(values[1]);
            if (surface == null)
                return null;
            ChartInfo info = (ChartInfo) values[2];
            MeshGeometry3D geometry = new MeshGeometry3D();
            int i = 2;
            i++;
            Point3D p1, p2, p3, p4;
            if (surface == area.Floor)
            {
                p1 = new Point3D(info.XMinimum, info.YMinimum, info.ZMinimum);
                p2 = new Point3D(info.XMaximum, info.YMinimum, info.ZMinimum);
                p3 = new Point3D(info.XMaximum, info.YMinimum, info.ZMaximum);
                p4 = new Point3D(info.XMinimum, info.YMinimum, info.ZMaximum);
            }
            else if (surface == area.BackWall)
            {
                p1 = new Point3D(info.XMinimum, info.YMinimum, info.ZMinimum);
                p2 = new Point3D(info.XMaximum, info.YMinimum, info.ZMinimum);
                p3 = new Point3D(info.XMaximum, info.YMaximum, info.ZMinimum);
                p4 = new Point3D(info.XMinimum, info.YMaximum, info.ZMinimum);
            }
            else if (surface == area.SideWall)
            {
                p1 = new Point3D(info.XMinimum, info.YMinimum, info.ZMinimum);
                p2 = new Point3D(info.XMinimum, info.YMaximum, info.ZMinimum);
                p3 = new Point3D(info.XMinimum, info.YMaximum, info.ZMaximum);
                p4 = new Point3D(info.XMinimum, info.YMinimum, info.ZMaximum);
            }
            else
                throw new NotSupportedException();

            Tessellator.TessellateDoubleSide(geometry, p1, p2, p3, p4);
            FillProperties p = StrictConverter.Convert<FillProperties>(values[i++]);
            ColorScheme colorScheme = StrictConverter.Convert<ColorScheme>(values[i++]);

            ChartStyle chartStyle = StrictConverter.Convert<ChartStyle>(values[i++]);
            Brush brush = ElementFillToBrushConverter.Convert(p, colorScheme, chartStyle, ChartElementType.Floor);
            GeometryModel3D result = new GeometryModel3D(geometry, new DiffuseMaterial(brush));
            ChartElement.SetObjectContext(result, values[0]);
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
